const { task, src, dest, series, parallel } = require('gulp');
const babel = require("gulp-babel");
const es2015Preset = require("babel-preset-es2015");

let uglify = require("gulp-uglify");//压缩js文件大小
let htmlminify = require('gulp-html-minify');//压缩HTML
let postcss = require('gulp-postcss');//用来启用Autoprefixer
let autoprefixer = require('autoprefixer');//自动为css添加浏览器前缀
let csso = require('gulp-csso');//压缩优化css
let smushit = require('gulp-smushit');//压缩率极高
let imagemin = require('gulp-imagemin');//仅支持png,jpg格式
let tinypng_nokey = require('gulp-tinypng-nokey');//免费,突破官网每月500张限制，提示友好
let rev = require('gulp-rev');
let revCollector = require('gulp-rev-collector');
let clean = require('gulp-clean');
let runSequence = require('run-sequence');  // 按顺序执行task


const FROM_FILES_DIR = '/Users/Believer/wc_web/yipai/yipai_web';
const CURR_FILES_DIR = 'copiedDist/';
const BUILD_DIST_DIR = 'yipai.min';

task("default", done => {
  console.log("gulp task default start")
  done()
})

task('clean', function () {
  // return src('build').pipe(clean());
});

/**
 * copy项目文件
 * base 参数:
 */
task('copy', done => {
  src(FROM_FILES_DIR + '/**/*')
    .pipe(dest(CURR_FILES_DIR))
    .on('end', () => {
      console.log("------------copy files succeed-------------")

      /**copy 字体文件 */
      src(CURR_FILES_DIR + 'src/fonts/*')
        .pipe(dest(BUILD_DIST_DIR + '/src/fonts'))
        .on('end', () => {
          console.log("------------copy font-files succeed-------------")
        })

      /**copy 部署配置文件 */
      src(CURR_FILES_DIR + 'now.json')
        .pipe(dest(BUILD_DIST_DIR))
        .on('end', () => {
          console.log("------------copy now.json succeed-------------")
        })
    });
  done()
})

/**
 * img 压缩1:tinypng_nokey，压缩率和成功率都还行
 */
task("miniImage", done => {
  // smushit({ verbose: true }) or imagemin({ progressive: true })
  src(CURR_FILES_DIR + 'src/pic/**/*.{jpg,jpeg,png,gif,ico}')
    .pipe(tinypng_nokey())
    .pipe(dest(BUILD_DIST_DIR + '/src/pic'))
    .on('end', () => {
      console.log("------------img minify succeed-------------")
    });
  done()
})

/**
 * img 压缩2:smushit ,压缩率高，但是不稳定，速度慢，容易中途出错
 */
task("miniImageHard", done => {
  // smushit({ verbose: true }) or imagemin({ progressive: true })
  src(CURR_FILES_DIR + 'src/pic/**/*.{jpg,jpeg,png,gif,ico}')
    .pipe(smushit({ verbose: true }))
    .pipe(dest(BUILD_DIST_DIR + '/src/pic'))
    .on('end', () => {
      console.log("------------img minify succeed-------------")
    });
  done()
})

/**
 * img 压缩3:imagemin ，压缩率低
 */
task("miniImageSoft", done => {
  src(CURR_FILES_DIR + 'src/pic/**/*.{jpg,jpeg,png,gif,ico}')
    .pipe(imagemin({ progressive: true }))
    .pipe(dest(BUILD_DIST_DIR + '/src/pic2'))
    .on('end', () => {
      console.log("------------img minify succeed-------------")
    });
  done()
})

/**
 * 个别图片压缩处理
 */
task("miniImageSome", done => {
  src(CURR_FILES_DIR + 'src/pic/case/case_10_*.{jpg,jpeg,png,gif,ico}')
    .pipe(tinypng_nokey())
    .pipe(dest(BUILD_DIST_DIR + '/src/pic/case'))
    .on('end', () => {
      console.log("------------img minify succeed-------------")
    });
  done()
})

/**
 * css 自动增加前缀并压缩
 */
task("miniCss", done => {
  src([CURR_FILES_DIR + 'src/css/*.css', '!*.min.css'])
    .pipe(rev())
    .pipe(postcss([autoprefixer({ overrideBrowserslist: ['last 2 versions'] })]))
    .pipe(csso())
    .pipe(dest(BUILD_DIST_DIR + '/src/css'))
    //生成rev-manifest.json
    .pipe(rev.manifest())
    .pipe(dest('rev/css'))
    .on('end', () => {
      console.log("------------css prefix&minify succeed-------------")
    });
  done()
})

/**
 * js ES6转换，压缩
 * # Babel 6 安装
 * npm install --save-dev gulp-babel@7 babel-core babel-preset-env
 */
task("miniJs", done => {
  src([CURR_FILES_DIR + '/src/js/*.js', '!*.min.js'])
    .pipe(rev())
    .pipe(babel({ presets: [es2015Preset] }))
    .pipe(uglify())
    .pipe(dest(BUILD_DIST_DIR + '/src/js'))
    //生成rev-manifest.json
    .pipe(rev.manifest())
    .pipe(dest('rev/js'))
    .on('end', () => {
      console.log("------------js minify succeed-------------")
    });
  done()
})

/**
 * html 压缩,替换加了版本号的css/js
 */
task("miniHtml", done => {
  src(['rev/**/*.json', CURR_FILES_DIR + '*.html'])
    .pipe(revCollector({
      replaceReved: true,
      dirReplacements: {
        'css': 'css',
        'js': 'js'
      }
    }))
    .pipe(htmlminify(
      {
        removeComments: true, //清除HTML注释
        collapseWhitespace: true, //压缩HTML
        minfyJS: true,//压缩JS
        minfyCss: true,//压缩CSS
      }
    ))
    .pipe(dest(BUILD_DIST_DIR)).on('end', () => {
      console.log("------------html minify succeed-------------")
    });
  done()
})

/**
 * 任务序列组
 */

// task("build", series('miniCss', 'miniJs', 'miniHtml', done => {
//   done();
// }))

// task("build", parallel('miniCss', 'miniJs', 'miniHtml', done => {
//   done();
// }))

// task('build', function (done) {
//   runSequence('clean', ['miniCss', 'miniJs'], 'miniHtml', done);
// });

task("build", parallel('miniCss', 'miniJs', done => {
  done();
}))

//1.拷贝源文件   gulp copy
//2.处理css/js  gulp build
//3.处理images  gulp miniImage
//4.处理html    gulp miniHtml